/**
 * 编辑FQA内容窗口
 * @author linzongxue 2012-2-26
 */
define(function(){
	var PostWindow = Ext.extend(Ext.Window, {
		modifiedId: null, //修改时，要修改的对象ID
		fqaType: null, //添加时，所属的fqa类型
		resizable: false,
		modal: true,
		width: 550,
	    animateTarget: cu.getDefaultAnimateTarget(),
        bodyStyle:'padding:3px;',
		iconCls: 'icon-window',
		initComponent: function(){
			PostWindow.superclass.initComponent.call(Ext.apply(this, {
				title: this.modifiedId ? '添加FQA' : '修改FQA',
				items: [this.formPanel = this.createFormPanel()],
				buttons: this.createButtons(),
				listeners: {
					show: function(){
						this.modifiedId && this.initFormData();
						this.getForm().firstFocus();
						cu.bindEnter(this, this.save);
					}
				}
			}));
		},
		save: function(continueToAdd){
			cu.submit({
				form: this.getForm(), 
				url: 'misc/fqa/savePost.do',
				success: function(data){
					if (this.callback){
						var typePath = this.getField('typeId').getValueNode().getPath();
						this.callback.call(this.cbScope, data, typePath);
					}
					if(continueToAdd){
						this.getField('question').setValue('');
						this.getField('answer').setValue('');
						this.getForm().clearInvalid();
					}
					else{
						this.close();
					}
				},
				scope: this
			});
		},
		initFormData: function(){
			cu.get({
				url:'misc/fqa/getPost.do', 
				params: {id: this.modifiedId},
				success: function(data){
					this.getForm().setValues(data);
				},
				scope: this
			});
		},
		createFormPanel: function(){
			var formPanel = new Ext.form.FormPanel({
			    baseCls: 'x-plain',
			    bodyStyle:'padding:5px;',
			    labelWidth: 56,
			    labelPad: 5,
			    labelAlign: "right",
			    defaults: {anchor:'99%', style:'margin-bottom:5px;'},
			    items: [
			        {xtype:'hidden', name:'id'},
			        {
						xtype:'combotree',
						fieldLabel:'* 类型',
						hiddenName: 'typeId',
						value: this.fqaType ? this.fqaType.id : null,
						height: 200,
						dataUrl: 'misc/fqa/getTypeChildren.do',
						nodePathUrl: 'sys/util/getEntityAttrPath.do?c=misc.FqaType',
						root: {id:'0', text:'根类型', expanded: true},
						rootVisible: false,
						rootSelectable: false,
						showFullPath: true,
						allowBlank: false
			        },{
						xtype:'textfield',
						name: 'question',
						fieldLabel: '* 问题',
						maxLength: 100, maxLengthText: '问题不能超过100个字',
						allowBlank: false
					},{
						xtype: 'textarea',
						name: 'answer',
						fieldLabel: '* 答案',
						allowBlank: false,
						height: 200
					}
			    ],
			    firstFocus: function(){
			    	this.findField('question').focus(false, 200);
			    }
			});
			return formPanel;
		},
		createButtons: function(){
			var buttons = [];
			if (!this.modifiedId){
				buttons.push({text: '保存并新增', width:90, handler: function(){this.save(true);}, iconCls: 'icon-save', scope: this});
			}
			buttons.push({text: '保存', width:60, handler: function(){this.save(false);}, iconCls: 'icon-save', scope: this});
			buttons.push({text: '关闭', width:60, handler: this.close, iconCls: 'icon-close', scope: this});
			return buttons;
		},
		getForm: function(){
			return this.formPanel.getForm();
		},
		getField: function(name){
			return this.getForm().findField(name);
		}
	});
	return PostWindow;
});